package com.egao.common.publicly.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.component.wechat.WeChatUtils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.guidance.entity.Lecture;
import com.egao.common.module.guidance.entity.LectureApply;
import com.egao.common.module.guidance.mapper.LectureApplyMapper;
import com.egao.common.module.guidance.mapper.LectureMapper;
import com.egao.common.module.guidance.service.LectureApplyService;
import com.egao.common.module.integral.constants.IntegralConstants;
import com.egao.common.module.integral.service.IntegralAggregateService;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.integral.entity.Integral;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.publicly.entity.ApplyButtonVO;
import com.egao.common.publicly.entity.MobileButtonVO;
import com.egao.common.publicly.mapper.PubliclyLectureSignInMapper;
import com.egao.common.publicly.service.PubliclyLectureSignInService;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 讲座签到接口实现类
 *
 * @author pyx
 * @date 2021/4/30
 */
@Service
public class PubliclyLectureSignInServiceImpl extends ServiceImpl<PubliclyLectureSignInMapper, Integral> implements PubliclyLectureSignInService {
    @Autowired
    private StudentService studentService;
    @Autowired
    private LectureApplyService lectureApplyService;
    @Autowired
    private IntegralAggregateService integralAggregateService;
    @Autowired(required = false)
    private LectureApplyMapper lectureApplyMapper;
    @Autowired(required = false)
    private LectureMapper lectureMapper;

    @Override
    public JsonResult signIn(String code, Integer lectureId) {
//        JsonResult codeResult = WeChatUtils.getStudent(code);
//        if(codeResult.getCode() == 1){
//            return codeResult;
//        }
//
//        String xh = (String) codeResult.get("number");
        String xh = "20212021";
        if (StrUtil.isBlank(xh)) {
            return JsonResult.error("未获取到学号");
        }
        Student student = studentService.getByXh(xh);

        if (student == null) {
            return JsonResult.error("未获取到学生信息");
        }

        MobileButtonVO mobileButtonVO = new MobileButtonVO();

        //根据studentId和lectureId获取讲座报名信息
        LectureApply lectureApply = lectureApplyService.studentApply(student.getId(), lectureId);
        if (lectureApply == null) {
            mobileButtonVO.setApplyButton(0);
            return JsonResult.ok("讲座未报名").put("mobileButtonVO", mobileButtonVO).put("studentId", student.getId());
        }

        switch (lectureApply.getState()) {
            case 0:
                return JsonResult.ok("待审核");
            case 2:
                return JsonResult.ok("审核不通过");
            case 3:
                return JsonResult.ok("已取消");
            default:
                return signInState(student.getId(), lectureId);
        }
    }

    @Override
    public JsonResult applyLecture(Integer studentId, Integer lectureId) {
        if (studentId == null) {
            return JsonResult.error("未获取到学生信息");
        }
        if (lectureId == null) {
            return JsonResult.error("未获取到讲座信息");
        }

        JsonResult jsonResult = lectureApplyService.applyVerify(lectureId, studentId);
        ApplyButtonVO applyButtonVO = (ApplyButtonVO) jsonResult.get("applyButton");

        if (applyButtonVO.getButtonState() != 0) {
            return JsonResult.ok(applyButtonVO.getButtonText());
        }

        return lectureApply(lectureId, studentId);
    }


    //讲座报名
    private JsonResult lectureApply(Integer lectureId, Integer studentId) {
        Lecture lecture = lectureMapper.selectById(lectureId);

        MobileButtonVO mobileButtonVO = new MobileButtonVO();
        if (lecture.getApplyPass() == 1) {
            LectureApply apply = new LectureApply();
            apply.setState(1);
            apply.setStudentId(studentId);
            apply.setLectureId(lectureId);
            lectureApplyMapper.insert(apply);

            //更新讲座报名人数信息
            Integer num = lectureApplyMapper.listGetLecture(lectureId, 1).size();
            lecture.setAuditApproveNum(num);
            lectureMapper.updateById(lecture);

            mobileButtonVO.setButtonState(0);
            mobileButtonVO.setSignInButton(0);
            return JsonResult.ok("报名成功").put("mobileButtonVO", mobileButtonVO);
        } else {
            LectureApply apply = new LectureApply();
            apply.setState(0);
            apply.setStudentId(studentId);
            apply.setLectureId(lectureId);
            lectureApplyMapper.insert(apply);

            //更新讲座报名人数信息
            Integer num = lectureApplyMapper.listGetLecture(lectureId, 0).size();
            lecture.setAuditAwaitNum(num);
            lectureMapper.updateById(lecture);

            mobileButtonVO.setButtonState(0);
            return JsonResult.ok("报名成功,请等待审核").put("mobileButtonVO", mobileButtonVO);
        }
    }

    //判断讲座是否已签到
    private JsonResult signInState(Integer studentId, Integer lectureId) {
        QueryWrapper<LectureApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId).eq("lecture_id", lectureId);

        MobileButtonVO mobileButtonVO = new MobileButtonVO();
        try {
            LectureApply lectureApply = lectureApplyMapper.selectOne(queryWrapper);

            if (lectureApply.getSignIn() == 0) {
                return JsonResult.ok("已签到");
            }

            Integral integral = new Integral();
            integral.setStudentId(studentId);
            integral.setType(1);
            integral.setBusinessId(lectureId);
            //积分数
            integral.setIntegral(IntegralConstants.INTEGRAL_LECTURE);
            baseMapper.insert(integral);

            //更新积分表
            integralAggregateService.updateIntegral(studentId, IntegralConstants.INTEGRAL_LECTURE);

            //更新报名表签到状态
            lectureApply.setSignIn(0);
            lectureApplyMapper.updateById(lectureApply);

            mobileButtonVO.setButtonState(0);
            return JsonResult.ok("签到成功").put("mobileButtonVO", mobileButtonVO);
        } catch (Exception e) {
            return JsonResult.error("数据异常");
        }
    }

}
